package com.mgxy;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DirectionComponent implements MyComponent {

    private JFrame frame;   //主窗口引用
    //定义起始点(startx,starty) 终点(endx,endy);
    private double start_x;
    private double start_y;
    private double end_x;
    private double end_y;
    private Double degree;
    private BallComponent whiteBall;

    private boolean isShow;

    public boolean getisShow() {
        return isShow;
    }

    public void setisShow(boolean show) {
        isShow = show;
    }

    public JFrame getFrame() {
        return frame;
    }

    public void setFrame(JFrame frame) {
        this.frame = frame;
    }

    public double getStart_x() {
        return start_x;
    }

    public void setStart_x(double start_x) {
        this.start_x = start_x;
    }

    public double getStart_y() {
        return start_y;
    }

    public void setStart_y(double start_y) {
        this.start_y = start_y;
    }

    public double getEnd_x() {
        return end_x;
    }

    public void setEnd_x(double end_x) {
        this.end_x = end_x;
    }

    public double getEnd_y() {
        return end_y;
    }

    public void setEnd_y(double end_y) {
        this.end_y = end_y;
    }

    public Double getDegree() {
        return degree;
    }

    public void setDegree(Double degree) {
        this.degree = degree;
    }

    public BallComponent getWhiteBall() {
        return whiteBall;
    }

    public void setWhiteBall(BallComponent whiteBall) {
        this.whiteBall = whiteBall;
    }

    public DirectionComponent(JFrame frame,BallComponent whiteBall){

        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1){
                    //让红线显示
                    isShow=true;
                    //获取白球心坐标  球心的计算从球左上方开始，所以需要加上球的一半
                    start_x = whiteBall.getX()+15;
                    start_y = whiteBall.getY()+12;
                    //获得终点坐标  鼠标点击区域
                    end_x = e.getX();
                    end_y = e.getY();
                    //获取向量角度   改变白球运动方向
                    whiteBall.setDegree(GameUtils.getAngle((int)start_x,(int)start_y,(int)end_x,(int)end_y));
                }else if (e.getButton() ==3){
                    whiteBall.setSpeed(20);
                    isShow = false;
                    //右键点击 清楚红线
                    start_x = 0;
                    start_y = 0;
                    end_x = 0;
                    end_y = 0;
                }
                super.mouseClicked(e);

            }
        });
    }



    @Override
    public void paintSelf(Graphics g) {

    }
}
